package com.example.liang.basamovie.json;

import android.util.Log;

import com.example.liang.basamovie.bean.FindFilmCasts;
import com.example.liang.basamovie.bean.FindFilmInfo;
import com.example.liang.basamovie.bean.FindFilmInfoResources;
import com.example.liang.basamovie.bean.FindFilmRelationArticle;
import com.example.liang.basamovie.bean.FindFilmUserComment;
import com.example.liang.basamovie.bean.FindMovieInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 电影详细信息的解析类
 */
public class FindFilmInfoJson {

    public static FindFilmInfo getFilmInfo(String path){
        try {
            JSONObject object=new JSONObject(path);
            //电影详细信息对象
            FindFilmInfo findFilmInfo=new FindFilmInfo();
            //影片信息对象
            FindFilmInfoResources findFilmInfoResources=new FindFilmInfoResources();

            List<FindFilmCasts> findFilmCastsList1=new ArrayList<>();//导演集合
            List<FindFilmCasts> findFilmCastsList2=new ArrayList<>();//演员集合
            FindFilmCasts findFilmCasts;
            //解析电影信息
            JSONObject filmResources = object.getJSONObject("filmResources");
            findFilmInfoResources.setSummary(filmResources.optString("summary"));
            findFilmInfoResources.setTitle(filmResources.optString("title"));
            findFilmInfoResources.setYear(filmResources.optString("year"));
            if (filmResources.has("posters")){
                JSONObject posters = filmResources.getJSONObject("posters");
                findFilmInfoResources.setImageUrl(posters.optString("large"));

            }
            //Log.e("sss","解析电影信息");
            if(filmResources.has("directors")){
                JSONArray directors = filmResources.getJSONArray("directors");
                for (int i = 0; i < directors.length(); i++) {
                    Log.e("sss","解析电影信息1");
                    findFilmCasts=new FindFilmCasts();
                    JSONObject jsonObject = directors.getJSONObject(i);
                    findFilmCasts.setName(jsonObject.optString("name"));
                    if(jsonObject.has("avatar")){
                        JSONObject avatar = jsonObject.getJSONObject("avatar");
                        findFilmCasts.setImage_url(avatar.optString("large"));
                    }

                    findFilmCastsList1.add(findFilmCasts);

                }

            }
            Log.e("sss","解析电影信息2");
            findFilmInfoResources.setFindFilmCastsList(findFilmCastsList1);
            findFilmInfo.setFindFilmInfoResources(findFilmInfoResources);
            //解析movieInfo
            JSONObject movieInfo = object.getJSONObject("movieInfo");
            JSONObject source = movieInfo.getJSONObject("_source");
            FindMovieInfo findMovieInfo=new FindMovieInfo();
            if(source.has("durations")){
                JSONArray durations = source.getJSONArray("durations");
                findMovieInfo.setDurations((String) durations.get(0));
            }
            if(source.has("genres")){
                JSONArray genres = source.getJSONArray("genres");
                findMovieInfo.setGenres((String) genres.get(0));
            }
            if(source.has("countries")){
                JSONArray countries = source.getJSONArray("countries");
                findMovieInfo.setCountries((String) countries.get(0));
            }
            if(source.has("pubdates")){
                JSONArray pubdates = source.getJSONArray("pubdates");
                findMovieInfo.setPubdates((String) pubdates.get(0));
            }
            if(source.has("original_title")){
                findMovieInfo.setTitle_pinyin(source.optString("original_title"));
            }

            findFilmInfo.setFindMovieInfo(findMovieInfo);

            Log.e("sss","解析演员");
            //解析演员
            JSONArray casts = source.getJSONArray("casts");
            for (int k = 0; k <casts.length() ; k++) {
                findFilmCasts=new FindFilmCasts();
                JSONObject jsonObject = casts.getJSONObject(k);
                findFilmCasts.setName(jsonObject.optString("name"));
                if(jsonObject.has("avatar_photos")){
                    JSONObject avatar_photos = jsonObject.getJSONObject("avatar_photos");
                    findFilmCasts.setImage_url(avatar_photos.optString("medium"));
                }

                findFilmCastsList2.add(findFilmCasts);
            }
            findFilmInfo.setFindFilmCastsList(findFilmCastsList2);//设置演员集合

            //解析　相关文章
            JSONArray relationArticle = object.getJSONArray("relationArticle");
            List<FindFilmRelationArticle> findFilmRelationArticleList=new ArrayList<>();
            FindFilmRelationArticle findFilmRelationArticle;
            //Log.e("sss","相关文章");
            for (int m = 0; m <relationArticle.length() ; m++) {
                JSONObject jsonObject = relationArticle.getJSONObject(m);
                findFilmRelationArticle=new FindFilmRelationArticle();
                findFilmRelationArticle.setImageUrl(jsonObject.optString("image"));
                findFilmRelationArticle.setSourceName(jsonObject.optString("sourceName"));
                findFilmRelationArticle.setTitle(jsonObject.optString("title"));
                findFilmRelationArticleList.add(findFilmRelationArticle);
            }
            findFilmInfo.setFindFilmRelationArticleList(findFilmRelationArticleList);
            //解析用户评论
            List<FindFilmUserComment> findFilmUserCommentList=new ArrayList<>();
            FindFilmUserComment findFilmUserComment;
            JSONArray commentList = object.getJSONArray("commentList");
            Log.e("sss","zheli");
            for (int i = 0; i <commentList.length() ; i++) {
                findFilmUserComment=new FindFilmUserComment();
                JSONObject jsonObject = commentList.getJSONObject(i);
                JSONObject commentinfo = jsonObject.getJSONObject("commentinfo");
                findFilmUserComment.setContent(commentinfo.optString("content"));
                findFilmUserComment.setPraisenum(commentinfo.optString("praisenum"));
                JSONObject userInfo = jsonObject.getJSONObject("userInfo");
                findFilmUserComment.setHeadImgUrl(userInfo.optString("headImgUrl"));
                findFilmUserComment.setNickname(userInfo.optString("nickname"));
                findFilmUserCommentList.add(findFilmUserComment);
            }
            findFilmInfo.setFindFilmUserCommentList(findFilmUserCommentList);
            Log.e("sd","没有出错"+findFilmUserCommentList.size());
        return findFilmInfo;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("error","解析出错");
        return null;
    }
}
